<template>
	<div class="home-box" style="margin-left: 65px">
	  <el-form :inline="true" :model="PageForm">
	    <el-form-item label="员工姓名">
	      <el-input v-model="PageForm.empName" placeholder="员工姓名" />
	    </el-form-item>
	    <el-form-item label="提成条目">
	      <el-input
	        v-model="PageForm.name"
	        placeholder="提成条目"
	        clearable
	      ></el-input>
	    </el-form-item>
	    <el-form-item>
	      <el-button @click="searchEvent" type="primary">查询 </el-button>
	    </el-form-item>
	    <el-form-item>
	      <el-button @click="update" type="primary">重置 </el-button>
	    </el-form-item>
	  </el-form>
	</div>
	
	
  <el-button
    type="primary"
    style="margin-left: 65px"
    plain
    icon="Plus"
    @click="handleAdd"
    >新增</el-button
  >
  <el-button
    color="#626aef"
    :dark="isDark"
    plain
    icon="Download"
    @click="handleUP"
    >导入</el-button
  >
  <!--<el-button color="#00BCD4" plain icon="Download" @click="handleUP"
    >导入</el-button
  >-->
  <el-tooltip class="item" effect="dark" content="刷新" placement="top">
    <el-button circle icon="Refresh" @click="update" />
  </el-tooltip>
  <el-table
    :data="EmpKpiList"
    border
    style="
      width: 90%;
      margin: 20px auto;
      border-radius: 8px;
      box-shadow: 5px 5px 10px rgba(64, 158, 255, 0.5);
    "
    :header-cell-style="{
      background: '#337ecc !important',
      color: '#ffffff',
      border: 'none !important',
      textAlign: 'center',
    }"
  >
    <el-table-column
      prop="empName"
      label="员工姓名"
      width="100"
    ></el-table-column>
    <el-table-column prop="name" label="提成条目" width="200"></el-table-column>
    <el-table-column
      prop="target1"
      label="条目一"
      width="100"
    ></el-table-column>
    <el-table-column prop="inTarget1" label="/￥" width="100"></el-table-column>
    <el-table-column
      prop="target2"
      label="条目二"
      width="100"
    ></el-table-column>
    <el-table-column prop="inTarget2" label="/￥" width="100"></el-table-column>
    <el-table-column
      prop="result"
      label="结算/￥"
      width="100"
    ></el-table-column>
    <el-table-column label="最后更新时间" width="220">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <el-icon><timer /></el-icon>
          <span style="margin-left: 10px">{{ scope.row.updateTime }}</span>
        </div>
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template #default="scope">
        <el-button
          size="small"
          @click="handleEdit(scope.$index, scope.row)"
          v-show="scope.row.state != 2"
          >编辑</el-button
        >
        <el-button
          size="small"
          type="danger"
          v-show="scope.row.state != 2"
          @click="handleDelete(scope.$index, scope.row)"
          >删除</el-button
        >
      </template>
    </el-table-column>
  </el-table>
  <pagination
    class="pagination"
    v-show="EmpKpiListPage.total > 0"
    :total="EmpKpiListPage.total"
    v-model:page="PageForm.page"
    v-model:limit="EmpKpiListPage.size"
    @pagination="handlePagination"
  />
  <Dialog
    v-model="dialogFormVisible"
    :CadreList="CadreList"
    :close="close"
    :update="update"
    :selectContent="selectContent"
    :type="type"
    :title="title"
  ></Dialog>
  <UploadExcel
    v-model="upload.open"
    :title="upload.upLoadTitle"
    :url="importUrl"
    :tipShow="true"
    @onSuccess="onSuccess"
    @onDownTemplate="onDownTemplate"
  ></UploadExcel>
</template>
<script setup>
import {
  ref,
  onMounted,
  onBeforeMount,
  reactive,
  defineEmits,
  watch,
} from "vue";
import { getEmpKpiList, deleteOneEmpKpi ,search} from "@/api/empKpi";
import { getEmpKpiTemplate } from "@/api/template";
import Dialog from "./components/kpiWageDialog";
import { ElMessage } from "element-plus";
import UploadExcel from "@/components/SendAndDownload/index";

const PageForm = ref({
	empName:"",
	name:"",
  page: 1,
  page_size: 10,
});

const searchEvent = () => {
  search(PageForm.value).then((data) => {
    EmpKpiListPage.value = data.data;
    EmpKpiList.value = data.data.records;
  });
};


const EmpKpiListPage = ref([]);
const EmpKpiList = ref([]);
onBeforeMount(() => {
  getEmpKpiList(PageForm.value).then((data) => {
    EmpKpiListPage.value = data.data;
    EmpKpiList.value = data.data.records;
  });
});
watch(EmpKpiList, (newVal) => {}, { deep: true });
const handlePagination = (value) => {
	
	
	if (PageForm.value.name == "" && PageForm.value.empName == "") {
	  getEmpKpiList(value).then((data) => {
	    EmpKpiListPage.value = data.data;
	    EmpKpiList.value = data.data.records;
	  });
	} else {
	  PageForm.value.page = value.page;
	  PageForm.value.page_size = value.page_size;
	  search(PageForm.value).then((data) => {
	    EmpKpiListPage.value = data.data;
	    EmpKpiList.value = data.data.records;
	  });
	}
	
	
  
};

const title = ref("新增提成项目");
const type = ref("add");
const selectContent = ref();
const dialogFormVisible = ref(false);
const handleAdd = () => {
  title.value = "新增提成项目";
  type.value = "add";
  dialogFormVisible.value = true;
  selectContent.value = {
    id: "",
    empId: "",
    positionId: "",
    kpiId: "",
  };
};
const handleEdit = (index, row) => {
  selectContent.value = row;
  title.value = "编辑提成项目";
  type.value = "edit";
  dialogFormVisible.value = true;
};
const handleDelete = (index, row) => {
  deleteOneEmpKpi({ id: row.empKpiId }).then((data) => {
    if (data.code == 200) {
      ElMessage({
        message: "删除成功",
        type: "success",
      });
      update();
    } else {
      ElMessage.error("删除失败");
    }
  });
};

const close = () => {
  dialogFormVisible.value = false;
};
const update = (form) => {
	PageForm.value.name = ""
	PageForm.value.empName = ""
  getEmpKpiList(PageForm.value).then((data) => {
    if (data.code == 200) {
      ElMessage({
        message: "更新成功",
        type: "success",
      });
      EmpKpiListPage.value = data.data;
      EmpKpiList.value = data.data.records;
    } else {
      ElMessage.error("更新失败");
    }
  });
};

const importUrl = ref("/EmpKpi/upload");
const upload = ref({
  open: false,
  upLoadTitle: "员工提成上传",
});
const onSuccess = (res) => {
  getEmpKpiList(PageForm.value).then((data) => {
    EmpKpiListPage.value = data.data;
    EmpKpiList.value = data.data.records;
  });
  upload.value.open = false;
};
const handleUP = () => {
  upload.value.open = true;
};
const onDownTemplate = () => {
  getEmpKpiTemplate();
};
</script>

<style lang="scss" scoped>
.title1 {
  color: rgb(64, 158, 255);
  width: 300px;
  height: 45px;
  font-size: 25px;
  background: rgba(64, 158, 255, 0.4);
  text-align: center;
  border-radius: 10px;
  margin: 10px auto 10px;
  line-height: 45px;
  border-top: #409eff 2px solid;
  border-bottom: #409eff 2px solid;
  box-shadow: 5px 5px 10px rgba(64, 158, 255, 0.5);
}
.pagination {
  margin: 20px 50px;
  display: flex;
  justify-content: end;
}
</style>
